<% import grails.persistence.Event %>
<% import org.codehaus.groovy.grails.commons.* %>
<% import edu.uow.devdocs.* %>
<%=packageName%>
<!doctype html>
<html>
	<head>
		<meta name="layout" content="main">
		<g:set var="entityName" value="\${message(code: '${domainClass.propertyName}.label', default: '${className}')}" />
		<title><g:message code="default.list.label" args="[entityName]" /></title>
	</head>
	<body>
		<div id="list-${domainClass.propertyName}" class="content scaffold-list" role="main">
		<p class="action_menu"><g:link class="${domainClass.propertyName}_create" action="create"><g:message code="default.new.label" args="[entityName]" /></g:link></p>
			<h1 id="${domainClass.propertyName}_list"><g:message code="default.list.label" args="[entityName]" /></h1>
			<g:flashMessage/>
			<table class="sortable tableForm">
				<thead>
					<tr>
					<%  excludedProps = Event.allEvents.toList() << 'id' << 'version' << 'lastUpdated' << 'lastUpdatedBy'
						allowedNames = domainClass.persistentProperties*.name << 'dateCreated' << 'lastUpdated'
				
			            def config = ConfigurationHolder.config
                 		key = 'ui_list_' + domainClass.propertyName
						props = []
						if (config.get(key) == null) {
							allowedNames = domainClass.persistentProperties*.name << 'dateCreated' << 'lastUpdated'
							props = domainClass.properties.findAll { allowedNames.contains(it.name) && !excludedProps.contains(it.name) && it.type != null && !Collection.isAssignableFrom(it.type) }
							Collections.sort(props, comparator.constructors[0].newInstance([domainClass] as Object[]))
						} else {
							allowedNames = config.get(key)
							def propList = domainClass.persistentProperties
							allowedNames.each {allowedName -> 
								prop = domainClass.getPropertyByName(allowedName)
				//println prop.type
								props.add(prop)
							 }
						}
				
						props.eachWithIndex { p, i ->
							if (i < 16) {
								if (p.isAssociation()) { %>
						<th><g:message code="${domainClass.propertyName}.${p.name}.label" default="${p.naturalName}" /></th>
					<%      } else { %>
						<g:sortableColumn property="${p.name}" title="\${message(code: '${domainClass.propertyName}.${p.name}.label', default: '${p.naturalName}')}" />
					<%  }   }   } %>
					 <th></th>
					</tr>
					<tr>
					<form action = "search">
					<% 
						props.eachWithIndex { p, i -> 
							renderSearchFieldForProperty(p, domainClass, prefix = "")
						}
					%>
					<th><button type="submit">Go</button></th></form></tr>
				</thead>
				<tbody>
				<g:each in="\${${propertyName}List}" status="i" var="${propertyName}">
					<tr>
					<%  props.eachWithIndex { p, i ->
							if (i == 0) { %>
						<td><g:link action="edit" id="\${${propertyName}.id}">\${fieldValue(bean: ${propertyName}, field: "${p.name}")}</g:link></td>
					<%      } else if (i < 16) {
								if (p.type == Boolean || p.type == boolean) { %>
						<td><g:formatBoolean boolean="\${${propertyName}.${p.name}}" /></td>
					<%          } else if (p.type == Date || p.type == java.sql.Date || p.type == java.sql.Time || p.type == Calendar) { %>
						<td><g:formatDate date="\${${propertyName}.${p.name}}" format="dd/MM/yyyy" /></td>
					<%          } else if (p.type == List || p.type == Set) { %>
						<td><g:formatDomainList list="\${${propertyName}.${p.name}}" /></td>
					<%          } else if (p.type == edu.uow.devdocs.Project ) { %>
						<td><g:link controller="project" action="edit" id="\${${propertyName}.id}">\${fieldValue(bean: ${propertyName}, field: "${p.name}")}</g:link></td>
					<%          } else if (p.type == edu.uow.devdocs.DevelopmentTask ) { %>
						<td><g:link controller="developmentTask" action="edit" id="\${${propertyName}.id}">\${fieldValue(bean: ${propertyName}, field: "${p.name}")}</g:link></td>
					<%          } else if (p.type == edu.uow.devdocs.Issue ) { %>
						<td><g:link controller="issue" action="edit" id="\${${propertyName}.id}">\${fieldValue(bean: ${propertyName}, field: "${p.name}")}</g:link></td>
					<%          } else if (p.type == edu.uow.devdocs.Deployment ) { %>
						<td><g:link controller="deployment" action="edit" id="\${${propertyName}.id}">\${fieldValue(bean: ${propertyName}, field: "${p.name}")}</g:link></td>
					<%          } else { %>
						<td>\${fieldValue(bean: ${propertyName}, field: "${p.name}")}</td>
					<%  }   }   } %>
					 <td></td>
					</tr>
				</g:each>
				</tbody>
			</table>
			<div class="pagination">
				<g:paginate total="\${${propertyName}Total}" />
			</div>
		</div>
	</body>
</html>

<%
private renderSearchFieldForProperty(property, owningClass, prefix = "") {
	boolean hasHibernate = pluginManager?.hasGrailsPlugin('hibernate')
	boolean display = true
	boolean required = false
	if (hasHibernate) {
		cp = owningClass.constrainedProperties[property.name]
		display = (cp ? cp.display : true)
		required = false
	}
	if (display) { %>

	<th> 
	<%
	if (property.type == Boolean || property.type == boolean ) {
		out << "<g:checkBox name=\"${property.name}\" value=\"\${params?.${property.name}}\" />"
	} else if (property.manyToOne || property.oneToOne) {
		if (property.association) {
			out << '<g:select'
			 out << ' id="' << property.name << '"'
			 out << ' name="' << property.name << '.id"'
			 if (property.name == 'project' && (owningClass.propertyName == 'developmentTask' || owningClass.propertyName == 'issue')) {
				 out << ' from="${userProjects}"'
			 } else {
			 	out << ' from="${' << property.type.name << '.list([cache: true])}"'
			 }
			 out << ' optionKey="id"'
			 out << ' value="${params?.' << "${property.name}" << '?.id}"'
			 out << ' class="many-to-one"'
			 out << " noSelection=\"['null': '']\""
			 out << '/>'
		}

	} else {
		out << "<g:textField name=\"${property.name}\" value=\"\${params?.${property.name}}\" />"
	}
	
	%>
	</th>

<%  }   }
 %>
